package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfComponentIp;
import com.yuandian.api.management.entity.ConfGraph;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.api.management.enums.GraphType;
import com.yuandian.management.mapper.ConfSegmentMapper;
import com.yuandian.management.service.ConfComponentIpService;
import com.yuandian.management.service.ConfGraphService;
import com.yuandian.management.service.ConfSegmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网端配置表-Service实现
 * @author zfsn
 * @date 2023-3-16
 */
@Service
@RequiredArgsConstructor
public class ConfSegmentServiceImpl extends ServiceImpl<ConfSegmentMapper, ConfSegment>
		implements ConfSegmentService {


	private final ConfComponentIpService confComponentIpService;


	private final ConfGraphService confGraphService;


	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRES_NEW)
	public ConfSegment getOneByWrapper(Wrapper<ConfSegment> queryWrapper) {
		return super.getOne(queryWrapper);
	}

	/**
	 * 删除节点
	 *
	 * @param confSegment
	 * @return 通用返回对象
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeNode(ConfSegment confSegment) {
//		1. 删除网端
		super.removeById(confSegment);
//		2. 删除组件ip
		LambdaUpdateWrapper<ConfComponentIp> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
		lambdaUpdateWrapper.eq(ConfComponentIp::getSegmentId, confSegment.getId());
		confComponentIpService.remove(lambdaUpdateWrapper);
		// 删除网端的端到端的关系
		LambdaUpdateWrapper<ConfGraph> lambdaUpdate = Wrappers.lambdaUpdate();
		lambdaUpdate.eq(ConfGraph::getType, GraphType.IT_ROAD_RELATIONSHIP.getType());
		lambdaUpdate.eq(ConfGraph::getGraphId, confSegment.getSystemId());
		lambdaUpdate.eq(ConfGraph::getNodeId, confSegment.getId());
		lambdaUpdate.eq(ConfGraph::getGraphId, confSegment.getSystemId());
		confGraphService.remove(lambdaUpdate);
		return true;
	}


}